package com.ruoyi.system.listener;

import com.ruoyi.common.modbus.data.MiiHeartBeatData;
import com.ruoyi.common.modbus.message.MiiMessage;
import com.ruoyi.common.modbus.utils.CacheShebeiUtils;
import com.ruoyi.common.modbus.utils.CacheUtils;
import com.takeoff.iot.modbus.netty.channel.MiiChannel;
import com.takeoff.iot.modbus.netty.data.HeartBeatData;
import com.takeoff.iot.modbus.netty.listener.MiiListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class HeartBeatListener implements MiiListener {

    @EventListener
    public void handleReceiveDataEvent(HeartBeatData data) {
        log.info("监听到心跳指令: "+ data.getCommand());
        log.info("设备号: "+ data.getDeviceGroup());
    }

    @Override
    public void receive(MiiChannel channel, MiiMessage message) {
        MiiHeartBeatData data = (MiiHeartBeatData) message.data();
        CacheShebeiUtils.put(message.deviceGroup(),data.shebei());
        CacheUtils.put(data.shebei(), channel,120000l);
        log.info("Netty通讯已绑定设备IP：" + channel.name() + message.deviceGroup() + data.shebei()+"会话地址"+channel);
    }
}
